//
//  LoginViewController.m
//  KurentoToolboxDemo
//
//  Created by 葛殿宏 on 16/3/19.
//  Copyright © 2016年 Telecom Italia S.p.A. All rights reserved.
//

#import "LoginViewController.h"
//#import "RoomVideoViewController.h"
#import "NBMRoomVideoViewController.h"

static  NSString* const kRoomURLString = @"RoomServerURL";

@interface LoginViewController ()

@property (nonatomic, strong) NBMRoom *room;

@property (strong, nonatomic) IBOutlet UITextField *roomNoTextField;
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.roomNoTextField.text = @"764902";
    self.usernameTextField.text = @"牛比比东";
    self.passwordTextField.text = @"111111";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

- (IBAction)loginButtonClick:(id)sender
{
    if (NULL_OR_EMPTY(self.roomNoTextField.text)) {
        [self toastTitle:@"提示" message:@"请输入房间号"];
        return;
    }

    if (NULL_OR_EMPTY(self.usernameTextField.text)) {
        [self toastTitle:@"提示" message:@"请输入用户名"];
        return;
    }
    
    if (NULL_OR_EMPTY(self.passwordTextField.text)) {
        [self toastTitle:@"提示" message:@"请输入密码"];
        return;
    }
    
    NSURL *roomURL = [NSURL URLWithString:DEFAULTWSROOM];
    
    NSString *roomURLString = [roomURL absoluteString];
    [self saveRoomServerURLString:roomURLString];

    self.room = [[NBMRoom alloc] initWithUsername:self.usernameTextField.text roomName:self.roomNoTextField.text roomURL:roomURL password:self.passwordTextField.text type:@"join"];
    
    NBMRoomVideoViewController *videoVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CallViewController"];
    videoVC.room = _room;
    
    [self presentViewController:videoVC animated:YES completion:nil];
}

#pragma mark - Defaults

- (void)saveRoomServerURLString:(NSString *)urlString {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:urlString forKey:kRoomURLString];
    [defaults synchronize];
}


@end
